<?php
class control_order extends core_action{
	
	function __construct(){
		if($this->isAdminLogin() === false){
			$this->redirict(Qtpl::createUrl('index', 'login',array(),'admin'));
		}
	}
	
	function index(){
		$model = new core_model_order();
		$page = intval($_REQUEST['p']) ? intval($_REQUEST['p']) : 1;
		$model->setPage($page);
		$model->setLimit(core_lib_constant::PAGE_NUM);
		$model->setCount(TRUE);
		switch ($_REQUEST['s']){
			case 1://未处理
				$condi = 'is_send=0 and cancel=0';
				break;
			case 2://已确认
				$condi = 'is_send=1';
				break;
			case 3://已取消
				$condi = 'cancel=1';
				break;
			default:
				$condi = '';
		}
		$rs = $model->select($condi,"","","order by oid desc");
		$params['order'] = $rs->items;
		$params['pageSize'] = $rs->pageSize;
		$params['page'] = $page;
		$params['order_cur'] = 'current';
		$params['token'] = md5($_COOKIE['adminKey']);
		$this->render('simpla/order_list.php', $params);
	}
	
	function send(){
		$oid = intval($_REQUEST['oid']);
		if($_GET['token'] != md5($_COOKIE['adminKey'])){
			$this->showMsg("参数错误",2);
		}
		$model = new core_model_order($oid);
		if($model->getData('is_send') == 1 or $model->getData('cancel') == 1){
			$this->showMsg("当前状态不能进行此操作",2);
		}
		$user = new core_service_user($model->getData('user_id'));
		if($user->sendGift($oid) === false){
			$this->showMsg("发货出错:".$user->getError(),2);
		}
		$model = new core_model_adminlog();
		$model->addLog(4, "发货了 {$model->getData('gift_name')}");
		$user->addNotice(2,"您兑换的礼物{$model->getData('gift_name')}已经发货！");
		$this->showMsg("操作成功",1,Qtpl::createUrl('order', 'index','','admin'));
	}
	
	function cancel(){
		$oid = intval($_REQUEST['oid']);
		if($_GET['token'] != md5($_COOKIE['adminKey'])){
			$this->showMsg("参数错误",2);
		}
		$model = new core_model_order($oid);
		if($model->getData('is_send') == 1 or $model->getData('cancel') == 1){
			$this->showMsg("当前状态不能进行此操作",2);
		}
		$user = new core_service_user($model->getData('user_id'));
		if($user->cancelExchange($oid) === false){
			$this->showMsg("取消出错:".$user->getError(),2);
		}
		$model = new core_model_adminlog();
		$model->addLog(4, "取消了发送 {$model->getData('gift_name')}");
		$user->addNotice(2,"您兑换的礼物{$model->getData('gift_name')}已经被取消！");
		$this->showMsg("操作成功",1,Qtpl::createUrl('order', 'index','','admin'));
	}
}